package com.junxonline.plat.controller.position;

import com.junxonline.common.dao.vo.*;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.position.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: PositionController
 * @Description: 部门岗位控制器
 */

@RestController
@RequestMapping(value = "/admin/position")
public class PositionController extends BaseController {

    @Autowired
    private PositionService positionService;

    /**
     * 获取部门岗位树数据
     * @return
     */
    @RequestMapping(value = "/getPositionTree")
    public ResultVO getPositionTree() {
        return getReturnSuccess(positionService.getPositionTree());
    }

    /**
     * 获取父级公司部门树数据
     * @return
     */
    @RequestMapping(value = "/getParentPositionTree")
    public ResultVO getParentPositionTree(@RequestBody PositionTypesVO vo) {
        return getReturnSuccess(positionService.getParentPositionTree(vo));
    }

    /**
     * 获取菜单数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getPosition")
    public ResultVO getMenu(@RequestBody QueryPositionVO vo) {

        if (CommonUtils.isEmpty(vo.getPositionId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(positionService.getPosition(vo));
    }

    /**
     * 筛选查询部门岗位
     * @param page
     * @param vo
     * @return
     */
    @RequestMapping(value = "/queryPosition")
    public PageResultVO queryPosition(PageVO page, QueryPositionVO vo) {
        return getReturnSuccess(positionService.queryPosition(page, vo));
    }


    /**
     * 添加部门岗位
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addPosition")
    public ResultVO addPosition(@RequestBody PositionVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getPositionType()) || CommonUtils.isEmpty(vo.getPositionLevel()) ||
            CommonUtils.isEmpty(vo.getPositionSort()) || CommonUtils.isEmpty(vo.getPositionStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        boolean success = positionService.addPosition(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

    /**
     * 修改部门岗位
     * @param vo
     * @return
     */
    @RequestMapping(value = "/editPosition")
    public ResultVO editPosition(@RequestBody PositionVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getPositionId()) || CommonUtils.isEmpty(vo.getPositionType()) ||
            CommonUtils.isEmpty(vo.getPositionLevel()) || CommonUtils.isEmpty(vo.getPositionSort()) || CommonUtils.isEmpty(vo.getPositionStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        // 判断选择的父级是否是自己
        if (vo.getPositionId().toString().equals(vo.getPositionParentId())) {
            return getReturnError(ErrorCodeEnum.POSITION_PARENT_NOT_SELF);
        }

        boolean success = positionService.editPosition(vo);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }


    /**
     * 删除部门岗位
     * @param vo
     * @return
     */
    @RequestMapping(value = "/delPosition")
    public ResultVO delPosition(@RequestBody PositionIdsVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getPositionIds())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] positionIdsArray = vo.getPositionIds();

        boolean success = positionService.delPosition(positionIdsArray);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

    /**
     * 设置权限
     * @param vo
     * @return
     */
    @RequestMapping(value = "/setPermission")
    public ResultVO setPermission(@RequestBody SetPermissionMultipleVO vo) {
        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuIds()) || CommonUtils.isEmpty(vo.getPositionIds())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long[] positionIdsArray = vo.getPositionIds();
        Long[] menuIdsArray = vo.getMenuIds();

        boolean success = positionService.setPermission(positionIdsArray, menuIdsArray);
        if (!success) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();
    }

}
